const connection = require('../app/database');

class MomentService {
    async getTypeList() {
        const statement = `select type.*, (select count(moment.type_id) from moment where moment.type_id=type.id) num from type Order By 'order' Desc; `;
        const [result] = await connection.execute(statement);

        return {
            code: 0,
            message: '',
            data: result
        };

    }
    async getTypeById(typeId) {
        const statement = `SELECT * FROM type WHERE id=?;`;

        const result = await connection.execute(statement, [typeId])

        return {
            code: 0,
            message: '',
            data: result[0][0]
        };
    }
    async create(type) {
        const { type_name, order } = type;
        console.log(type_name, order);
        const statement = 'INSERT INTO `type` (`type_name`, `order`) VALUES (?, ?)';

        const result = await connection.execute(statement, [type_name, order])

        return result;
    }

    async edit(type) {
        const { type_name, order, id } = type;
        const statement = 'UPDATE `type` SET `type_name` = ?,`order` = ? WHERE id = ?;';

        const result = await connection.execute(statement, [type_name, order, id])

        return result;
    }
    async remove(typeId) {
        const statement = 'DELETE FROM type WHERE id = ?;';

        const result = await connection.execute(statement, [typeId])

        return result
    }
}

module.exports = new MomentService();